<template>
	<view class="mainpadding">
		<u-picker :show="xhshow" ref="uPicker" title="选择品牌-车型" keyName="name" :columns="columns" @confirm="confirm" @cancel="cancel"
			@change="changeHandler"></u-picker>
		<!-- <u-picker :show="xhshow" ref="uPicker" :columns="columns" @confirm="confirm" @change="changeHandler"></u-picker> -->
		<view class="mainpadding ffffff flexbetween" @click="xhshow=true">
			<view class="sanshier fonweight">品牌-车型</view>
			<view class="flexright">
				<input class="textright" placeholder="请选择" disabled="" type="text" v-model="user_car_type_text" />
				<u-icon name="arrow-right" color="#C8C8C8"></u-icon>
			</view>
		</view>
		<view class="mainpadding ffffff margin_top flexbetween">
			<view class="sanshier fonweight">名称</view>
			<input class="textright" placeholder="请输入名称" type="text" v-model="name" />
		</view>
		<view class="mainpadding ffffff margin_top flexbetween">
			<view class="sanshier fonweight">车牌照</view>
			<input class="textright" placeholder="请输入车牌照" type="text" v-model="carno" />
		</view>
		<view class="ffffff radius mainpadding margin_top">
			<view class="sanshier fonweight">车架号图片</view>
			<view class="flexleft flex_wrap ">
				<view class="flexcenter sanshis" v-if="image_text" @click="uploadImg">
					<view class="shantp dingwei  margin_top">
						<image :src="image_text" mode=""></image>
					</view>
				</view>
				<view class="flexcenter sanshis" v-if="image_text==''" @click="uploadImg">
					<view class="huibox margin_top">
						<view class="shangctp"></view>
					</view>
				</view>
			</view>
		</view>

		<view class="gudingdb  mainpadding">
			<view class="bigbtn" @click="submit">确认</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				user_car_type_id: "",
				user_car_type_text: "",
				user_car_type_pid: "",
				name: "",
				carno: "",
				image: "",
				image_text: "",
				columns: [],
				columnData: [],
				xhshow: false,
				listid:"",
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				this.getDetail(options.id)
			}
			this.getcx()
		},
		methods: {
			getDetail(id){
				httpRequest.request('/api/user/userCarDetail', 'GET', {id}, false, false, true).then(res => {
					let data = res.data
					this.user_car_type_id = data.type.id
					this.user_car_type_pid = data.type1.id
					this.user_car_type_text = data.type.name+"-"+data.type1.name
					this.name = data.name
					this.carno = data.carno
					this.image = data.image
					this.image_text = data.image_text
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			submit() {
				if(this.user_car_type_id==""){
					httpRequest.toast("请选择品牌-车型")
					return false
				}
				if(this.name==""){
					httpRequest.toast("请输入名称")
					return false
				}
				if(this.carno==""){
					httpRequest.toast("请输入车牌照")
					return false
				}
				if(this.image==""){
					httpRequest.toast("请上传车架号照片")
					return false
				}
				let data = {
					user_car_type_id: this.user_car_type_id,
					user_car_type_pid: this.user_car_type_pid,
					name: this.name,
					carno: this.carno,
					image: this.image
				}
				let url = "/api/user/addUserCar"
				if(this.listid){
					this.$set(data,"id",this.listid)
					url = '/api/user/editUserCar'
				}
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					httpRequest.toast(res.msg);
					setTimeout(() => {
						uni.navigateBack(1)
					}, 1000)
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			getcx() {
				httpRequest.request('/api/user/userCarTypeIndex', 'GET', {}, false, false, true).then(res => {
					this.columns[0] = res.data
					this.columns[1] = res.data[0].child
					res.data.forEach((item, index) => {
						this.columnData[index] = item.child
					})
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// changeHandler(e) {
			// 	const {
			// 		columnIndex,
			// 		value,
			// 		values, // values为当前变化列的数组内容
			// 		index,
			// 		// 微信小程序无法将picker实例传出来，只能通过ref操作
			// 		picker = this.$refs.uPicker
			// 	} = e
			// 	// 当第一列值发生变化时，变化第二列(后一列)对应的选项
			// 	if (columnIndex === 0) {
			// 		// picker为选择器this实例，变化第二列对应的选项
			// 		picker.setColumnValues(1, this.columnData[index])
			// 	}
			// },
			// // 回调参数为包含columnIndex、value、values
			// confirm(e) {
			// 	console.log('confirm', e)
			// 	this.cxshow = false
			// },
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, this.columnData[index])
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				this.user_car_type_pid = e.value[0].id
				this.user_car_type_id = e.value[1].id
				this.user_car_type_text = e.value[0].name + "-" + e.value[1].name
				this.xhshow = false
			},
			cancel() {
				this.xhshow = false
			},
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.image = res.url;
					this.image_text = image_item

				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.huibox {
		width: 200rpx;
		height: 200rpx;
		background: #F6F6F6;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
	}

	.shantp {
		width: 200rpx;
		height: 200rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangctp {
		width: 26rpx;
		height: 26rpx;
		background-size: 100% 100%;
		background-image: url('@/static/images/system/shangchuantp.png');
	}

	.shanchu {
		width: 32rpx;
		height: 32rpx;
		position: absolute;
		top: 0;
		right: 0;
		background-size: 100% 100%;
		background-image: url('@/static/images/system/shanchu1.png');
	}
</style>